//
//  CinemaListViewController.m
//  REMenuExample
//
//  Created by Roman Efimov on 2/20/13.
//  Copyright (c) 2013 Roman Efimov. All rights reserved.
//

#import "CinemaListViewController.h"
#import "DeviceUtil.h"
#import "BuyChooseViewController.h"
#import "NetworkAlertView.h"


@implementation CinemaListViewController
{
    ExpandTableModel *_expandModel;
    
    NSMutableArray *_nameList;
    NSMutableArray *_boollist;
    
    NSMutableArray *_movieInfoArray;
    NSMutableArray *_cinemaArray;
    
    NSMutableArray *_savingItemArray;
}

@synthesize filmId;
@synthesize filmName;

- (void) dealloc
{
    [_expandModel release];
    [_nameList release];
    [_boollist release];
    [_movieInfoArray release];
    [_cinemaArray release];
    [_savingItemArray release];
    
    _expandModel = nil;
    _nameList = nil;
    _boollist = nil;
    _movieInfoArray = nil;
    _cinemaArray = nil;
    _savingItemArray = nil;
    
    [super dealloc];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //等待网络
    NetworkAlertView *networking = [NetworkAlertView shareView];
    [networking showNetView];
    
    //请求电影详情
    _movieInfoArray = [[NSMutableArray alloc]init];
    MovieManager *moviemanager = [MovieManager shareManager];
    moviemanager.delegate = self;
    [moviemanager qryMovieDetail:filmId];
    
    _expandModel = [[ExpandTableModel alloc] init];
    _cinemaArray = [[NSMutableArray alloc]init];
    _savingItemArray = [[NSMutableArray alloc]init];

}

- (void)showMenu
{
    if (_menu.isOpen)
        return [_menu close];
//    
//    // Sample icons from http://icons8.com/download-free-icons-for-ios-tab-bar
//    
//    REMenuItem *allAreaItem = [[REMenuItem alloc] initWithTitle:@"广州市区"
//                                                          image:nil
//                                               highlightedImage:nil
//                                                         action:^(REMenuItem *item) {
//                                                             NSArray *array = [[[NSArray alloc] initWithObjects:
//                                                                                [ExpandItem modelWithString:@"飞扬影院\n芳村区正佳广场飞扬影院" ETMChildrentr:@[@"15:10   高清3D   正佳1厅   ￥160",@"15:10   普通高清   正佳2厅   ￥80"]],
//                                                                                [ExpandItem modelWithString:@"保利影院\n芳村区万凌汇广场保利影院" ETMChildrentr:@[@"15:10   高清3D   保利1厅   ￥160",@"15:10   普通高清   保利2厅   ￥80"]],
//                                                                                nil]autorelease];
//                                                             _expandModel.rootItems = array;
//                                                             [self.tableView reloadData];
//                                                         }];
//
//    _menu = [[REMenu alloc] initWithItems:@[allAreaItem]];
//    _menu.cornerRadius = 1;
//    _menu.shadowColor = [UIColor blackColor];
//    _menu.shadowOffset = CGSizeMake(0, 1);
//    _menu.shadowOpacity = 1;
//    _menu.imageOffset = CGSizeMake(0, -1);
    
    [_menu showFromNavigationController:self.navigationController];
}

#pragma mark -
#pragma mark Rotation

- (BOOL)shouldAutorotate
{
    return YES;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
    return YES;
}


//列表展示
#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ExpandItem *item = (ExpandItem *)[_expandModel itemAtRow:indexPath.row];
    if (item.ETMParent) {
        return 40;
    }else{
        return 60;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _expandModel.numOfRows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    
    ExpandItem *item = (ExpandItem *)[_expandModel itemAtRow:indexPath.row];
    if (item.ETMParent) {
        cell.contentView.backgroundColor = [UIColor whiteColor];
        cell.textLabel.font = [UIFont systemFontOfSize:15];
    }else{
        cell.contentView.backgroundColor = [UIColor lightGrayColor];
        cell.backgroundColor = [UIColor grayColor];
        cell.textLabel.backgroundColor = [UIColor clearColor];
        cell.textLabel.font = [UIFont systemFontOfSize:18];
    }
    cell.textLabel.text = item.title;
    cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
    cell.textLabel.numberOfLines = 0;
    return cell;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{    
    ExpandItem *item = (ExpandItem *)[_expandModel itemAtRow:indexPath.row];
    if (item.ETMParent) {
        
        //进行购票阶段
        BuyChooseViewController *chooseController = [[BuyChooseViewController alloc] init];
        chooseController.filmId = filmId;
        chooseController.filmName = filmName;
        
        NSArray *arrWriterPos =[item.title componentsSeparatedByString:NSLocalizedString(@"       ", nil)];
        chooseController.time = [arrWriterPos objectAtIndex:0];
        chooseController.price = [arrWriterPos objectAtIndex:3];
        
        ExpandItem *parent = (ExpandItem*)item.ETMParent;
        NSString *buyTitle = [NSString stringWithFormat:@"%@%@", parent.title, item.title];
        for (int i = 0; i < [_savingItemArray count]; i ++) {
            SavingItemInfo *iteminfo = [_savingItemArray objectAtIndex:i];
            if ([iteminfo.title isEqualToString:buyTitle]) {
                chooseController.foretellId = iteminfo.foretellId;
                chooseController.hallId = iteminfo.hallId;
                chooseController.hallName = iteminfo.hallName;
                chooseController.cinemaName = iteminfo.cinemaName;
                chooseController.cinemaId = iteminfo.cinemaId;
                chooseController.fees = iteminfo.fees;
            }
        }
        
        [self.navigationController pushViewController:chooseController animated:YES];
        [chooseController release];
    }else{
        [_expandModel tableView:tableView didSelectRowAtIndexPath:indexPath];
        [tableView deselectRowAtIndexPath:indexPath animated:NO];
    }
}


#pragma network

//查看所有影院
- (void)qryDetailMoviesFinish:(BOOL)isSuccess Movies:(NSMutableArray*)movieInfoArray;
{
    _movieInfoArray = movieInfoArray;
    
    for (int i = 0; i < [movieInfoArray count]; i++) {
        DetailMovieInfo *movieinfo = [movieInfoArray objectAtIndex:i];
        
        //请求影院 
        MovieManager *moviemanager = [MovieManager shareManager];
        moviemanager.delegate = self;
        [moviemanager qryCinemaDetail:filmId cinemaId:movieinfo.cinemaId];
    }
}

//查看影院详情
- (void)qryCinemaFinish:(BOOL)isSuccess Cinema:(CinemaInfo*)cinemaArray
{
    for (int i = 0; i < [_movieInfoArray count]; i++) {
        DetailMovieInfo *movieinfo = [_movieInfoArray objectAtIndex:i];
        if ([movieinfo.cinemaId isEqualToString:cinemaArray.cinemaId]) {
            //加载大图标
            ItemInfo *iteminfo = [[ItemInfo alloc]init];
            iteminfo.title = [NSString stringWithFormat:@"%@\n%@", movieinfo.region, movieinfo.cinemaName];
            
            //加载内容
            NSMutableArray *contentArray = [[NSMutableArray alloc]init];
            for (int j = 0; j < [cinemaArray.ShowList count]; j++) {
                ShowInfo *showInfo = [cinemaArray.ShowList objectAtIndex:j];
                //加载时间
                NSMutableString *showTime = [[NSMutableString alloc] initWithString:showInfo.showtime];
                [showTime insertString:@"：" atIndex:2];
                
                //加载价格
                NSString *price = [NSString stringWithFormat:@"￥%@",[showInfo.price substringToIndex:2]];
                
                NSInteger intprice = [[price substringFromIndex:1] intValue]*100 + [showInfo.fee intValue];
                NSString *content = [NSString stringWithFormat:@"%@       %@分钟       %@       ￥%d",
                                     showTime,
                                     showInfo.duration,
                                     showInfo.hallName,
                                     intprice/100];
                
                [contentArray addObject:content];
                
                //暂存信息，作转发用
                SavingItemInfo *tempSavingInfo = [[SavingItemInfo alloc]init];
                tempSavingInfo.title = [NSString stringWithFormat:@"%@%@",iteminfo.title,content];
                tempSavingInfo.foretellId = showInfo.foretellId;
                tempSavingInfo.hallId = showInfo.hallId;
                tempSavingInfo.hallName = showInfo.hallName;
                tempSavingInfo.cinemaId = movieinfo.cinemaId;
                tempSavingInfo.cinemaName = movieinfo.cinemaName;
                tempSavingInfo.fees = showInfo.fee;
                [_savingItemArray addObject:tempSavingInfo];
            }
            iteminfo.content = contentArray;
            [contentArray release];
            
            [_cinemaArray addObject:iteminfo];
            
            [iteminfo release];
        }
    }
    
    //取消等待
    NetworkAlertView *networking = [NetworkAlertView shareView];
    [networking delView];
    
    //reload界面
    NSMutableArray *rootitems = [[NSMutableArray alloc]init];
    for (int i = 0; i < [_cinemaArray count]; i++) {
        ItemInfo *iteminfo = [_cinemaArray objectAtIndex:i];
        ExpandItem *expandItem = [ExpandItem modelWithString:iteminfo.title ETMChildrentr:iteminfo.content];
        [rootitems addObject:expandItem];
    }
    _expandModel.rootItems = rootitems;

    [self.tableView reloadData];
    
    [rootitems release];
}

@end

@implementation ItemInfo
@end

@implementation SavingItemInfo
@end



